// pages/evaluate/evaluate/evaluate.js
var http = require('../../../utils/server.js');
var util = require('../../../utils/util.js');
Page({
  data: {
    imgs: [],
    score: 0,
    anonymity: 0,
    scoreZhArr: ['非常差', '较差', '一般', '较好', '非常好'],
    selectTags: [],
    selectTagsIndex: [],
  },

  onLoad: function(option) {
    var that = this
    that.setData({
      order_id: option.order_id || "",
      product_id: option.product_id || ""
    })
    console.log(option)
    http.post({
      url: "product/getEvaluateInfo",
      order_id: that.data.order_id,
      product_id: that.data.product_id
    }, function(data) {
      console.log('result', data)
      that.setData({
        product_info: data.product_info,
        shopkeeper: data.shopkeeper,
        tags: data.tags
      })
    })
  },
  checkboxChange: function(e) {
    var that = this
    var anonymity = 0
    if (e.detail.value.length > 0)
      anonymity = 1
    that.setData({
      anonymity: anonymity
    })
  },
  bindTextAreaBlur: function(e) {
    var that = this
    var content = e.detail.value
    that.setData({
      content: content
    })
  },
  chooseScore: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    that.setData({
      score: index + 1
    })

  },
  selectTag: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var selectTags = that.data.selectTags
    var selectTagsIndex = that.data.selectTagsIndex
    var sindex = selectTags.indexOf(index)
    if (sindex > -1) {
      selectTagsIndex[index] = false
      selectTags.splice(sindex, 1)
    } else {
      selectTagsIndex[index] = true
      selectTags.push(index)
    }
    that.setData({
      selectTags: selectTags,
      selectTagsIndex
    })

  },
  addPic: function(e) {
    var that = this
    if (that.data.imgs.length > 9) {
      util.showModal("最多只能上传10张")
      return false;
    }
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: function(res) {
        var tempFilePaths = res.tempFilePaths
        var imgs = that.data.imgs
        imgs.unshift({
          src: tempFilePaths[0],
          cover: true,
          coverHeight: 100
        })
        that.setData({
          imgs: imgs
        })

        var token = wx.getStorageSync('token') || "";
        const uploadTask = wx.uploadFile({
          url: 'https://www.ymqvip.com/api/upload/uploadPic',
          filePath: tempFilePaths[0],
          name: 'imgfile',
          formData: {
            'token': token
          },
          success: function(res) {
            var uploadData = JSON.parse(res.data)
            if (uploadData.code == 0) {
              console.log('上传成功', uploadData.data)
              var imgs = that.data.imgs
              imgs[0].src = uploadData.data
              imgs[0].cover = false
              that.setData({
                imgs: imgs
              })
            } else {
              util.showModal("上传出错，" + res.message)
              return false;
            }
          },
          fail: function(e) {
            console.log('上传出错', e)
            util.showModal("上传出错，请联系管理员。")
            return false;
          }
        })

        uploadTask.onProgressUpdate((res) => {
          var imgs = that.data.imgs
          imgs[0].coverHeight = 100 - res.progress
          that.setData({
            imgs: imgs
          })
        })

      }
    })

  },
  delPic: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var imgs = that.data.imgs
    imgs.splice(index, 1)
    that.setData({
      imgs: imgs
    })
  },
  confirm: function(e) {
    var that = this
    var pics = [];
    for (p in that.data.imgs) {
      pics.push(that.data.imgs[p].src)
    }
    http.post({
      url: "product/evaluate",
      order_id: that.data.order_id,
      product_id: that.data.product_id,
      score: that.data.score,
      content: that.data.content,
      tags: that.data.selectTags,
      anonymous: that.data.anonymity,
      pics: pics
    }, function(data) {
      util.showSuccess('评价成功', function() {
        wx.navigateBack({
          delta: 1
        })
      })
    })
  },
})